var uid, cid;
var gid = readData(localStorage.guideId);
var max = document.documentElement.clientWidth - 30;
var initDaoId;
var flag = true;
//获得内容对象
function guanzhu($scope, $http) {
	//根据导购ID从后台请求圈子信息
	$scope.isshow = false;
	var add = document.getElementById("add");
	var remove = document.getElementById("remove")
	if (localStorage.account) { //已登录
		uid = readData(localStorage.account);
		datas = {
			accountId: uid,
			userId: gid
		}
	} else { //未登录
		datas = {
			userId: gid
		}
	}
	var urlDetail = getUrl("circleDetail");
	$http({
		method: 'POST',
		url: urlDetail,
		timeout: 10000,
		params: datas
	}).success(function(data, status, headers, config) {
		//圈子信息
		if (data.code == "0") {
			$scope.objs = data.obj;
			$scope.circles = data.obj.circle; //获得圈子对象			
			$scope.shop = data.obj.marketShop; //圈子详细信息
			$scope.guides = data.obj.userList; //服务顾问列表
			console.log(JSON.stringify(data));
			for (var i = 0; i < $scope.guides.length; i++) {
				if ($scope.guides[i].attention == 'true') {
					$scope.guides[i].cname = 'active';
					add.style.display = "none";
					remove.style.display = "block";
					$scope.isshow = true;
					flag = false;
					localStorage.guideId = writeData($scope.guides[i].userid);
					localStorage.groupId = writeData($scope.guides[i].circleid);
				}
			}
			if (flag) {
				add.style.display = "block";
				remove.style.display = "none";
				$scope.guides[0].cname = 'active';
				gid = $scope.guides[0].userid;
			}
		} else {
			mui.toast('获取的导购二维码不存在，请重新扫描');
			mui.back();
		}
	}).error(function() {
		console.log('网络链接异常，请稍后重试(e)');
	})

	$scope.selectAdvisor = function(obj) { //选择服务顾问
		if ($scope.isshow) {
			mui.toast('请先取消关注后再选择其他顾问');
			return;
		}
		gid = obj.userid;
		cid = obj.circleid;
		var div = document.getElementById('serviceAdvisor').getElementsByClassName('active');
		div[0].className = "";
		document.getElementById(gid).className = 'active';
	}
	$scope.addCircle = function() { //加关注
		if (checkin()) {
			plus.nativeUI.showWaiting("请稍等");
			var urlAdd = getUrl("guanzhu");
			var headerAdd = getHeader();
			$http({
				method: 'POST',
				url: urlAdd,
				headers: headerAdd,
				timeout: 10000,
				data: 'accountId=' + readData(localStorage.account) + '&userId=' + gid
			}).success(function(data, status, headers, config) {
				plus.nativeUI.closeWaiting()
				if (data.code == "0") {
					document.getElementById("add").style.display = "none";
					document.getElementById("remove").style.display = "block";
					var num = parseInt(document.getElementById(gid + 'gw').innerHTML) + 1;
					document.getElementById(gid + 'gw').innerHTML = num;
					$scope.isshow = true;
					localStorage.guideId = writeData(gid);
					localStorage.groupId = writeData(cid);
					refresh();
					//closeWindow();
				} else if (data.code == "6") {
					mui.alert(data.msg, '同一商户下只可以关注一个导购', function() {});
				} else {
					mui.toast('网络链接异常，请稍后重试(e)');
				}
			}).error(function(data, status, headers, config) {
				plus.nativeUI.closeWaiting()
				mui.toast('网络链接异常，请稍后重试(e)');
			})
		}
	}
	$scope.removeCircle = function() { //取消关注
		if (checkin()) {
			plus.nativeUI.showWaiting("请稍等");
			var urlCancle = getUrl("cancelAttention");
			var headerCancle = getHeader();
			$http({
				method: 'POST',
				url: urlCancle,
				headers: headerCancle,
				timeout: 10000,
				data: 'accountId=' + readData(localStorage.account) + '&userId=' + gid
			}).success(function(data, status, headers, config) {
				plus.nativeUI.closeWaiting()
				if (data.code != "0") {
					mui.alert('网络连接异常，请稍后重试' + data.code, '提示', function() {});
				} else {
					document.getElementById("add").style.display = "block";
					document.getElementById("remove").style.display = "none";
					var num = parseInt(document.getElementById(gid + 'gw').innerHTML) - 1;
					document.getElementById(gid + 'gw').innerHTML = num;
					$scope.isshow = false;
					refresh();
					//closeWindow();
				}
			}).error(function(data, status) {
				plus.nativeUI.closeWaiting()
				mui.toast('网络链接异常，请稍后重试(e)');
			})
		}
	}
	$scope.openCircle = function(cname, cId) { //打开圈子	
		localStorage.circleName = writeData(cname)
		mui.openWindow({
			url: 'circleMain.html',
			id: 'circleMain.html',
			waiting: {
				autoShow: false
			}
		});
	}
}

function refresh() {

	shuaxin("brandBuying.html", "classifySearch.html", "productSearch.html", "hotSales.html", "market.html");
	reloadHtml("home.html", "homeMarket.html", "personal.html");
}

function shuaxin() {
	for (var i = 0; i < arguments.length; i++) {
		var veiw = plus.webview.getWebviewById(arguments[i]);
		if (veiw) {
			mui.fire(veiw, "reloads");
			//veiw.evalJS("init()");
		}
	}
}

function closeWindow() {
	for (var i = 0; i < arguments.length; i++) {
		var veiw = plus.webview.getWebviewById('search.html');
		if (veiw) {
			veiw.close();
		}
	}
}

function checkin() {
	if (localStorage.account) {
		return true;
	} else {
		mui.openWindow({
			url: '../my/loginHead.html',
			id: 'loginHead.html',
			waiting: {
				autoShow: false
			}
		});
	}
}